package com.example.thecharacterbook;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements Filterable {
    private  Context context;
    Activity activity;
    private final ArrayList Word_id;
    private final ArrayList Mean;
    private ArrayList Example, Example2, Example3;
    private final ArrayList Word;
    ArrayList<String> WordAll, Word_id_ALL, MeanAll, ExampleAll, Example2All, Example3ALL;

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.activity_holder_view, parent, false);
        return new MyViewHolder(view);
    }

    MyAdapter(Activity activity, Context context, ArrayList Word_id, ArrayList Word, ArrayList Mean, ArrayList Example, ArrayList Example2, ArrayList Example3) {
        this.activity = activity;
        this.context = context;
        this.Word_id = Word_id;
        this.Word = Word;
        this.Mean = Mean;
        this.Example = Example;
        this.Example2 = Example2;
        this.Example3 = Example3;
        WordAll = new ArrayList<>(Word);
//        MeanAll = new ArrayList<>(Mean);
//        Word_id_ALL = new ArrayList<>(Word_id);
//        ExampleAll = new ArrayList<>(Example);
//        ExampleAll = new ArrayList<>(Example2);
//        Example3ALL = new ArrayList<>(Example3);
        Log.e("context", "IS" + Word_id);
    }

    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.Word_txt.setText(String.valueOf(Word.get(position)));
        holder.Word_Id_txt.setText(String.valueOf(Word_id.get(position)));
        holder.Mean_txt.setText(String.valueOf(Mean.get(position)));
        int a = 0;
        Log.e("EXAMPLE1:", String.valueOf(Example.get(position)));
        Log.e("EXAMPLE2:", String.valueOf(Example2.get(position)));
        Log.e("EXAMPLE3:", String.valueOf(Example3.get(position)));
        if (!String.valueOf(Example.get(position)).equals("null"))
            a++;
        if (!String.valueOf(Example2.get(position)).equals("null"))
            a++;
        if (!String.valueOf(Example3.get(position)).equals("null"))
            a++;
        holder.Number.setText("共有" + a + "条例句");
        holder.TheViewHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CHECKWORD.class);
                intent.putExtra("Word", String.valueOf(Word.get(position)));
                intent.putExtra("Word_Id", String.valueOf(Word_id.get(position)));
                intent.putExtra("Mean", String.valueOf(Mean.get(position)));
                intent.putExtra("Example", String.valueOf(Example.get(position)));
                intent.putExtra("Example2", String.valueOf(Example2.get(position)));
                intent.putExtra("Example3", String.valueOf(Example3.get(position)));
                activity.startActivityForResult(intent, 1);
            }
        });

    }

    @Override
    public int getItemCount() {

        Log.e("context", "IS" + Word_id);
        return Word.size();
    }

    @Override
    public Filter getFilter() {
        return filter;
    }

    public Filter filter = new Filter() {
//        FilterResults filterID = new FilterResults();
//        FilterResults  filterMean = new FilterResults();
//        FilterResults   filterE1 = new FilterResults();
//        FilterResults  filterE2 = new FilterResults();
//        FilterResults   filterE3 = new FilterResults();
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            ArrayList<String> list = new ArrayList<>();
//            ArrayList<String>  list_mean = new ArrayList<>();
//            ArrayList<String> list_id = new ArrayList<>();
//            ArrayList<String>  list_example = new ArrayList<>();
//            ArrayList<String>  list_example2 = new ArrayList<>();
//            ArrayList<String>  list_example3 = new ArrayList<>();
            if (charSequence.toString().isEmpty()) {
      list.addAll(WordAll);
//      list_mean.addAll(MeanAll);
//      list_id.addAll(Word_id_ALL);
//      list_example.addAll(ExampleAll);
//      list_example2.addAll(Example2All);
//      list_example3.addAll(Example3ALL);
            } else {
                for (String words : WordAll) {
                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
                        list.add(words);
                }
//                for (String words : MeanAll) {
//                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
//                        list_mean.add(words);
//                }
//                for (String words : Word_id_ALL) {
//                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
//                        list_id.add(words);
//                }
//                for (String words : ExampleAll) {
//                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
//                        list_example.add(words);
//                }
//                for (String words : Example2All) {
//                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
//                        list_example2.add(words);
//                }
//                for (String words : Example3ALL) {
//                    if (words.toLowerCase(Locale.ROOT).contains(charSequence.toString().toLowerCase(Locale.ROOT)))
//                        list_example3.add(words);
//                }
            }
            FilterResults  filterResults = new FilterResults();
            filterResults.values = list;
//            filterID.values = list_id;
//            filterMean.values = list_mean;
//            filterE1.values = list_example;
//            filterE2.values = list_example2;
//            filterE3.values = list_example3;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
           Word.clear();
//        Mean.clear();
//        Example2.clear();
//        Example3.clear();
//        Example.clear();
//        Word_id.clear();
//            Word_id.addAll((Collection) filterID.values);
//            Mean.addAll((Collection) filterMean.values);
//            Example.addAll((Collection)filterE1.values);
//            Example2.addAll((Collection)filterE1.values);
//            Example3.addAll((Collection)filterE1.values);
            Word.addAll((Collection) filterResults.values);
            notifyDataSetChanged();
        }
    };

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView Word_Id_txt, Word_txt, Mean_txt, Number;
        LinearLayout TheViewHolder;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            Log.e("111", "awdwad");
            Word_Id_txt = itemView.findViewById(R.id.WordID123);
            Word_txt = itemView.findViewById(R.id.Word123);
            Mean_txt = itemView.findViewById(R.id.example123);
            Number = itemView.findViewById(R.id.TheSizeNumber);
            TheViewHolder = itemView.findViewById(R.id.TheHolderView);

        }
    }
}
